// Learn cc.Class:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] https://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html
const PuzzleManager = require('PuzzleManager')
cc.Class({
    extends: cc.Component,

    properties: {
        stonePhoto:{
            type:[cc.SpriteFrame],
            default:[],
            tooltip:'红绿蓝三块石头的图片'
        },
        sprite:cc.Sprite,
        pm: {
            type:PuzzleManager,
            default:null,
            tooltip:'解谜管理器'
        },
        id:1,
        commands: {
            type:[cc.Node],
            default:[],
            tooltip:'指令集'
        },
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        this.stone = -1;
    },

    start () {

    },
    
    onBeginContact: function (contact, selfCollider, otherCollider) {
        if (otherCollider.tag == 8) {
            //与主角相碰，有石头
            if (this.stone != -1) {
                this.commands[3].active = true;
            } else {
                var stones = this.pm.collectedStones;
                for (let i = 0; i < 3; i++) {
                    this.commands[i].active = stones[i];
                }
            }
        }
    },

    onEndContact: function (contact, selfCollider, otherCollider) {
        if (otherCollider.tag == 8) {
            //与主角相碰
            for (let i = 0; i < 4; i++) {
                this.commands[i].active = false;
            }
        }
    },

    setStone (target, data) {
        this.pm.useStone(data, this.id);
        this.sprite.spriteFrame = this.stonePhoto[data];
        this.sprite.node.active = true;
        this.stone = data;
        for (let i = 0; i < 3; i++) {
            this.commands[i].active = false;
        };
        this.commands[3].active = true;
    },
    
    removeStone (target, data) {
        this.pm.removeStone(this.stone, this.id);
        this.sprite.node.active = false;
        this.stone = -1;
        var stones = this.pm.collectedStones;
        for (let i = 0; i < 3; i++) {
            this.commands[i].active = stones[i];
        };
        this.commands[3].active = false;
    },
    
    start () {

    },
    
    // update (dt) {},
});
